package com.yuan.blog.controller;

import com.yuan.blog.dto.UserToken;
import com.yuan.blog.security.UserTokenThreadLocal;
import com.yuan.blog.service.ISysLoginService;
import com.yuan.blog.service.impl.TokenService;
import com.yuan.blog.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @program: blog-system
 * @description:
 * @author: yuan_boss
 * @create: 2022-12-28 14:31
 **/
@RestController
@RequestMapping("/system")
public class SysLoginController {


    @Autowired
    private ISysLoginService loginService;

    @Autowired
    private TokenService tokenService;

    @PostMapping("/login")
    public R login(@RequestBody Map<String,String> map){
        String username = map.get("username");
        String password = map.get("password");

        if (ObjectUtils.isEmpty(username) || ObjectUtils.isEmpty(password)){
            return R.error("用户名或密码不能为空");
        }

        String token = loginService.login(username, password);

        return R.ok().put("token",token);

    }

    @GetMapping("/info")
    public R info(){
        return R.ok().put("data", UserTokenThreadLocal.get());
    }

    @GetMapping("/logout")
    public R logout(){
        loginService.logout();
        return R.ok();
    }
}
